<?php
/**
 * Created by PhpStorm.
 * User: peter
 * Date: 18-9-27
 * Time: 上午10:41
 */

namespace app\club\logic;


use app\model\ActivityCollectionPoint;
use app\model\ActivityGroupStage;
use extend\Functions;
use extend\STATUS_CODE;

class Activity
{
    /**
     * @purpose 批量修改上车点人数限制
     * @param array $data
     * @return array
     * @throws \Exception
     */
    public static function batchSetCollectionPointNumberOfPeople (array $data): array
    {
        foreach ($data as $key => $val) {
            if (!isset($val['sign_up_limit']) || empty($val['sign_up_limit']) || (int)$val['sign_up_limit'] < 1) {
                return Functions::result(
                    STATUS_CODE::PARAMETER_ERROR,
                    '第' . $key . '条数据参数错误'
                );
                break;
            }
        }
        $model  = new ActivityCollectionPoint();
        $result     = $model
            ->isUpdate(true)
            ->saveAll($data);
        if ($result) {
            return Functions::result();
        }
        return Functions::result(
            STATUS_CODE::UPDATE_FAIL,
            '保存失败'
        );
    }
    /**
     * @purpose 添加集合点
     * @param array $data
     * @return array
     */
    public static function createCollectionPoint (array $data): array
    {
        if (!isset($data['set_out_time']) || empty($data['set_out_time'])) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '缺少上车时间'
            );
        }
        if (!isset($data['sign_up_limit']) || empty($data['sign_up_limit'])) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '缺少人数限制'
            );
        }
        $data['set_out_time'] = strtotime($data['set_out_time']);
        $model      = new ActivityCollectionPoint();
        $result     = $model
            ->isUpdate(false)
            ->allowField(true)
            ->data($data)
            ->save();
        if ($result) {
            return Functions::result();
        }
        return Functions::result(
            STATUS_CODE::CREATE_FAIL,
            '添加失败'
        );
    }
    /**
     * @purpose 通过团期ID获取集合点
     * @param int $group_stage_id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function collectionPointItems(int $group_stage_id): array
    {
        $model = new ActivityCollectionPoint();
        $list  = $model
            ->where('activity_group_stage_id', $group_stage_id)
            ->field('id,activity_group_stage_id,province_name,city_id,lng,lat,city_name,county_name,address,title,
            FROM_UNIXTIME(set_out_time, "%Y-%m-%d %H:%i:%s") as set_out_time,sign_up_limit')
            ->select();
        return Functions::result(
            STATUS_CODE::SUCCESS,
            $list
        );
    }

    /**
     * @purpose 编辑集合点
     * @param array $data
     * @return array
     */
    public static function editCollectionPoint (array $data): array
    {
        if (!isset($data['id']) || empty($data['id'])) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '参数错误'
            );
        }
        if (isset($data['set_out_time'])) {
            $data['set_out_time'] = strtotime($data['set_out_time']);
        }
        $model      = new ActivityCollectionPoint();
        $result     = $model
            ->isUpdate(true)
            ->allowField(true)
            ->save($data, ['id' => $data['id']]);
        if ($result) {
            return Functions::result(
                STATUS_CODE::SUCCESS,
                '编辑成功'
            );
        }
        return Functions::result(
            STATUS_CODE::UPDATE_FAIL,
            '编辑失败'
        );
    }
    /**
     * @purpose 活动发布
     * @param array $data
     * @param int $club_id
     * @return array
     * @throws \think\exception\PDOException
     * @throws \Exception
     */
    public static function add(array $data, int $club_id): array
    {

        $activity_data = self::checkActivityData($data, $club_id);
        if (isset($activity_data['code'])) {
            return $activity_data;
        }
        $activity         = new \app\model\Activity();
        $group_stage      = new ActivityGroupStage();
        $collection_point = new ActivityCollectionPoint();
        $activity->startTrans();
        $activity_result = $activity
            ->isUpdate(false)
            ->data($activity_data)
            ->save();
        $activity_id     = (int)$activity->id;
        // 构建团期数据
        if (!isset($data['group_stage']) || empty($data['group_stage'])) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '请至少添加一个团期'
            );
        }
        if (isset($data['activity']['status']) && in_array((int)$data['activity']['status'], [1, 99])) {
            $status_data['status'] = (int)$data['activity']['status'];
        }
        $group_stage_data = [];
        foreach ($data['group_stage'] as $key => $item) {
            if (!isset($item['start_time']) || empty($item['start_time'])) {
                return Functions::result(
                    STATUS_CODE::PARAMETER_ERROR,
                    '第' . $key . '个团期缺少开始日期'
                );
                break;
            }
            if (!isset($item['end_time']) || empty($item['end_time'])) {
                return Functions::result(
                    STATUS_CODE::PARAMETER_ERROR,
                    '第' . $key . '个团期缺少结束日期'
                );
                break;
            }
            if (!isset($item['apply_end_date']) || empty($item['apply_end_date'])) {
                return Functions::result(
                    STATUS_CODE::PARAMETER_ERROR,
                    '第' . $key . '个团期缺少截止报名日期'
                );
                break;
            }
            if (!isset($item['adult_price']) || empty($item['adult_price'])) {
                return Functions::result(
                    STATUS_CODE::PARAMETER_ERROR,
                    '第' . $key . '个团期缺少成人价格'
                );
                break;
            }
            if (!isset($item['kid_price']) || empty($item['kid_price'])) {
                return Functions::result(
                    STATUS_CODE::PARAMETER_ERROR,
                    '第' . $key . '个团期缺少儿童价格'
                );
                break;
            }
            if (!isset($item['insurance_type']) || empty($item['insurance_type'])) {
                return Functions::result(
                    STATUS_CODE::PARAMETER_ERROR,
                    '第' . $key . '个团期缺少保险类型'
                );
                break;
            }
            if (!isset($item['insurance_price']) || empty($item['insurance_price'])) {
                return Functions::result(
                    STATUS_CODE::PARAMETER_ERROR,
                    '第' . $key . '个团期缺少保险价格'
                );
                break;
            }
            array_push($group_stage_data, [
                'activity_id'      => $activity_id,
                'start_time'       => strtotime($item['start_time']),
                'end_time'         => strtotime($item['end_time']),
                'apply_end_date'   => strtotime($item['apply_end_date']),
                'adult_price'      => ((float)$item['adult_price']) * 100,
                'kid_price'        => ((float)$item['kid_price']) * 100,
                'insurance_type'   => (int)$item['insurance_type'],
                'insurance_price'  => ((float)$item['insurance_price']) * 100,
                'lng'              => (string)$data['activity']['lng'],
                'lat'              => (string)$data['activity']['lat'],
                'status'           => $status_data['status'],
                'collection_point' => $item['collection_point'],
            ]);
        }
        $group_stage_result    = $group_stage
            ->isUpdate(false)
            ->allowField(true)
            ->saveAll($group_stage_data);
        $collection_point_data = [];
        foreach ($group_stage_result as $k => $_group_stage) {
            if (!empty($_group_stage->collection_point)) {
                foreach ($_group_stage->collection_point as $collection) {
                    array_push($collection_point_data, [
                        'activity_group_stage_id' => (int)$_group_stage->id,
                        'province_id'             => (int)$collection['province_id'],
                        'city_id'                 => (int)$collection['city_id'],
                        'county_id'               => (int)$collection['county_id'],
                        'province_code'           => (string)$collection['province_code'],
                        'city_code'               => (string)$collection['city_code'],
                        'county_code'             => (string)$collection['county_code'],
                        'title'                   => (string)$collection['title'],
                        'province_name'           => (string)$collection['province_name'],
                        'city_name'               => (string)$collection['city_name'],
                        'county_name'             => (string)$collection['county_name'],
                        'address'                 => (string)$collection['address'],
                        'lng'                     => (string)$collection['lng'],
                        'lat'                     => (string)$collection['lat'],
                        'set_out_time'            => strtotime($collection['set_out_time']),
                        'sign_up_limit'           => (int)$collection['sign_up_limit'],
                    ]);
                }
            }
        }

        $collection_point_result = $collection_point
            ->isUpdate(false)
            ->saveAll($collection_point_data);

        if ($activity_result &&
            count($group_stage_data) === $group_stage_result->count() &&
            count($collection_point_data) === $collection_point_result->count()
        ) {
            $activity->commit();
            return Functions::result(
                STATUS_CODE::SUCCESS,
                '保存成功'
            );
        }
        $activity->rollback();
        return Functions::result(
            STATUS_CODE::CREATE_FAIL,
            '保存失败'
        );
    }

    /**
     * @purpose 检测数据
     * @param $data
     * @param $club_id
     * @return array
     */
    private static function checkActivityData($data, $club_id): array
    {
        if (!$club_id) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '参数错误!'
            );
        }
        if (!isset($data['address_info']) || empty($data['address_info'])) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '区域信息错误!'
            );
        }
        $address_info = $data['address_info'];
        if (
            !isset($address_info['province']) ||
            empty($address_info['province']) ||
            !isset($address_info['city']) ||
            empty($address_info['city'])
        ) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '区域信息错误'
            );
        }
        if (!isset($data['activity']) || empty($data['activity'])) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '缺少活动基础信息'
            );
        }
        $base_info = $data['activity'];
        if (!isset($base_info['title']) || empty($base_info['title'])) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '缺少标题'
            );
        }
        if (!isset($base_info['destination']) || empty($base_info['destination'])) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '缺少活动目的地'
            );
        }
        if (!isset($base_info['difficulty']) || empty($base_info['difficulty'])) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '缺少活动难度系数'
            );
        }
        if ((int)$base_info['difficulty'] <= 0 || (int)$base_info['difficulty'] > 10) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '活动难度系数错误'
            );
        }
        if (!isset($base_info['slider']) || empty($base_info['slider'])) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '缺少轮播图'
            );
        }
        if (!isset($base_info['thumb']) || empty($base_info['thumb'])) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '缺少封面图'
            );
        }
        if (isset($base_info['descriptions'])) {
            $activity_data['descriptions'] = $base_info['descriptions'];
        }
        if (!isset($base_info['way_type']) || !in_array((int)$base_info['way_type'], [1, 2, 3])) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '活动类型错误'
            );
        }
        if (!isset($base_info['light_spot']) || empty($base_info['light_spot'])) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '缺少活动亮点'
            );
        }
        if (!isset($base_info['graphic']) || empty($base_info['graphic'])) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '缺少图文详情'
            );
        }
        if (!isset($base_info['trip_graphic']) || empty($base_info['trip_graphic'])) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '缺少行程介绍'
            );
        }
        if (!isset($base_info['leader_graphic']) || empty($base_info['leader_graphic'])) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '缺少领队介绍'
            );
        }
        if (!isset($base_info['activity_notice']) || empty($base_info['activity_notice'])) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '缺少活动须知'
            );
        }
        if (!isset($base_info['lng']) && !isset($base_info['lat'])) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '缺少位置信息'
            );
        }
        $activity_data            = [];
        $activity_data['club_id'] = $club_id;
        foreach ($address_info as $key => $value) {
            $activity_data[$key . '_id']   = isset($value['id']) ? (int)$value['id'] : '';
            $activity_data[$key . '_name'] = isset($value['name']) ? (string)$value['name'] : '';
            $activity_data[$key . '_code'] = isset($value['code']) ? (string)$value['code'] : '';
        }
        $activity_data['title']           = (string)$base_info['title'];
        $activity_data['destination']     = (string)$base_info['destination'];
        $activity_data['difficulty']      = (int)$base_info['difficulty'];
        $activity_data['slider']          = (array)$base_info['slider'];
        $activity_data['thumb']           = (string)$base_info['thumb'];
        $activity_data['way_type']        = (int)$base_info['way_type'];
        $activity_data['light_spot']      = (array)$base_info['light_spot'];
        $activity_data['graphic']         = (string)$base_info['graphic'];
        $activity_data['trip_graphic']    = (string)$base_info['trip_graphic'];
        $activity_data['leader_graphic']  = (string)$base_info['leader_graphic'];
        $activity_data['activity_notice'] = (string)$base_info['activity_notice'];
        // 把lng lat放到团期中
        $activity_data['descriptions']    = (string)$base_info['descriptions'];
        if (isset($base_info['category'])) {
            $activity_data['category'] = (int)$base_info['category'];
        }
        return $activity_data;
    }

    /**
     * @purpose 获取活动列表
     * @param array $where
     * @param int $page
     * @param int $size
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function activityItems(
        array $where = [],
        int $page = 1,
        int $size = 50
    ): array
    {
        $group_stage = new \app\model\ActivityGroupStage();
        $total       = $group_stage
            ->alias('gs')
            ->join('activity a', 'gs.activity_id=a.id')
            ->where($where)
            ->count(1);
        $fields      = 'a.id as activity_id,a.province_id,a.city_id,a.county_id,a.province_name,
        a.city_name,a.county_name,a.title,a.destination,a.difficulty,a.descriptions,
        a.thumb,a.way_type,gs.status,gs.id as group_stage_id,
        FROM_UNIXTIME(gs.start_time, "%Y.%m.%d") as start_date,
        FROM_UNIXTIME(gs.start_time, "%Y%m%d") as stage_num,
        (FLOOR(((gs.end_time - gs.start_time) / 86400)) + 1) as days,
        FROM_UNIXTIME(gs.end_time,"%Y.%m.%d") as end_date,
        FROM_UNIXTIME(gs.apply_end_date, "%Y.%m.%d %H:%i") as apply_end_date,gs.adult_price,
        (select sum(sign_up_limit) as sign_up_limit from activity_collection_point where activity_group_stage_id=gs.id) as sign_up_limit,
        gs.kid_price,gs.insurance_type,gs.insurance_price';
        $list        = $group_stage
            ->where($where)
            ->alias('gs')
            ->join('activity a', 'gs.activity_id=a.id')
            ->page($page, $size)
            ->order([
                'gs.create_at' => 'desc',
            ])
            ->field($fields)
            ->select();
        return Functions::result(
            STATUS_CODE::SUCCESS,
            [
                'list'  => $list,
                'total' => $total,
            ]
        );
    }

    /**
     * @purpose 获取单条活动数据(用于修改本条活动信息)
     * @param int $id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function activitySingleData (int $id): array
    {
        if ($id === -1) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '参数错误'
            );
        }
        $fields = 'id,title,destination,difficulty,descriptions,
        slider,thumb,way_type,light_spot,graphic,
        leader_graphic,trip_graphic,activity_notice,category';
        $model  = new \app\model\Activity();
        $result = $model
            ->where('id',$id)
            ->field($fields)
            ->find();
        $activity_data = [];
        $activity_data['activity'] = $result;
        if ($result) {
            return Functions::result(
                STATUS_CODE::SUCCESS,
                $activity_data
            );
        }
        return Functions::result(
            STATUS_CODE::FAIL,
            '获取失败'
        );
    }

    /**
     * @purpose 通过活动ID修改活动信息
     * @param array $data
     * @return array
     */
    public static function activityEdit(array $data): array
    {
        if (!isset($data['id']) || empty($data['id'])) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '缺少活动ID!'
            );
        }
        $activity_data = [
            'title'           => (string) $data['title'],
            'destination'     => (string) $data['destination'],
            'difficulty'      => (string) $data['difficulty'],
            'descriptions'    => (string) $data['descriptions'],
            'slider'          => (array) $data['slider'],
            'thumb'           => (string) $data['thumb'],
            'way_type'        => (string) $data['way_type'],
            'light_spot'      => (array) $data['light_spot'],
            'graphic'         => (string) $data['graphic'],
            'leader_graphic'  => (string) $data['leader_graphic'],
            'trip_graphic'    => (string) $data['trip_graphic'],
            'activity_notice' => (string) $data['activity_notice'],
        ];
        // activity数据表数据插入
        $model = new \app\model\Activity();
        // 数据更新
        $result = $model
            ->isUpdate(true)
            ->save($activity_data, ['id' => $data['id']]);
        if ($result) {
            return Functions::result(
                STATUS_CODE::SUCCESS,
                '活动修改成功!'
            );
        }
        return Functions::result(
            STATUS_CODE::FAIL,
            '活动修改失败!'
        );

    }

    /**
     * @purpose 修改活动状态
     * @param array $data
     * @return array
     */
    public static function editUpperAndLower (array $data): array
    {
        if (!isset($data['id']) || empty($data['id'])) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '团期ID错误!'
            );
        }
        if (!isset($data['status'])
            || empty($data['status'])
            || !in_array((int)$data['status'],[1,99])
        ) {
           return Functions::result(
               STATUS_CODE::PARAMETER_ERROR,
               '活动状态值错误!'
           );
        }
        $model  = new \app\model\ActivityGroupStage();
        $result = $model
            ->isUpdate(true)
            ->save($data);
        if ($result) {
            return Functions::result(
                STATUS_CODE::SUCCESS,
                '活动状态修改成功!'
            );
        }
        return Functions::result(
            STATUS_CODE::FAIL,
            '活动状态修改失败!'
        );

    }

    /**
     * @purpose 获取单条团期数据
     * @param int $id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function groupStageSingleData(int $id): array
    {
        if ($id === -1) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '参数错误'
            );
        }
        $fields = 'id,start_time,end_time,apply_end_date,adult_price,kid_price,
        insurance_type,insurance_price,status';
        $model = new \app\model\ActivityGroupStage();
        $result = $model
            ->where('id',$id)
            ->field($fields)
            ->find();
        $group_stage_data = [];
        $group_stage_data['id'] = $result['id'];
        $group_stage_data['start_time'] = date('Y-m-d',$result['start_time']);
        $group_stage_data['end_time'] = date('Y-m-d',$result['end_time']);
        $group_stage_data['apply_end_date'] = date('Y-m-d H:i:s',$result['apply_end_date']);
        $group_stage_data['adult_price'] = $result['adult_price']/100;
        $group_stage_data['kid_price'] = $result['kid_price']/100;
        $group_stage_data['insurance_price'] = $result['insurance_price']/100;
        $group_stage_data['insurance_type'] = $result['insurance_type'];
        $group_stage_data['status'] = $result['status'];
        if ($result) {
            return Functions::result(
                STATUS_CODE::SUCCESS,
                $group_stage_data
            );
        }
        return Functions::result(
            STATUS_CODE::FAIL,
            '获取失败'
        );

    }
    /**
     * @purpose 通过团期ID修改团期信息
     * @param array $data
     * @return array
     */
    public static function groupStageEdit(array $data): array
    {
        if (!isset($data['id']) || empty($data['id'])) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '缺少团期ID!'
            );
        }
        // 获取修改信息
        $group_stage_data = [
            'start_time'      => strtotime($data['start_time']),
            'end_time'        => strtotime($data['end_time']),
            'adult_price'     => ((int) $data['adult_price'])*100,
            'kid_price'       => ((int) $data['kid_price'])*100,
            'insurance_type'  => (int) $data['insurance_type'],
            'insurance_price' => ((int) $data['insurance_price'])*100,
            'status'          => (int) $data['status']
        ];
        $model            = new \app\model\ActivityGroupStage();
        $result           = $model
            ->isUpdate(true)
            ->save($group_stage_data,['id' => $data['id']]);
        if ($result) {
            return Functions::result(
                STATUS_CODE::SUCCESS,
                '团期修改成功!'
            );
        }
        return Functions::result(
            STATUS_CODE::FAIL,
            '团期修改失败!'
        );
    }

    /**
     * aa
     * @purpose 通过活动ID获取团期列表
     * @param int $activity_id
     * @param int $page
     * @param int $size
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function groupItems($activity_id,
                                      int $page,
                                      int $size): array
    {
        if ($activity_id == null) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '缺少活动ID'
            );
        }
        $model  = new \app\model\ActivityGroupStage();
        $field  = 'id,start_time,end_time,adult_price,kid_price,insurance_type,insurance_price,lng,lat';
        $result = $model
            ->where('activity_id', (int)$activity_id)
            ->field($field)
            ->page($page, $size)
            ->order('create_at desc')
            ->select();
        // 将时间戳转换成时间格式
        foreach ($result as $value) {
            $value['start_time'] = date('Y-m-d', $value['start_time']);
            $value['end_time']   = date('Y-m-d', $value['end_time']);
        }
        $total = $result->count();
        return Functions::result(
            STATUS_CODE::SUCCESS,
            [
                'total' => $total,
                'list'  => $result,
            ]
        );
    }

    /**
     * @purpose 通过团期ID获取集合点信息
     * @param int $activity_group_stage_id
     * @param int $page
     * @param int $size
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function collectionItems($activity_group_stage_id,
                                           int $page,
                                           int $size): array
    {
        if ($activity_group_stage_id == null) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '缺少团期ID'
            );
        }
        $model  = new \app\model\ActivityCollectionPoint();
        $field  = 'id,address,lng,lat,title,set_out_time,sign_up_limit';
        $result = $model
            ->where('activity_group_stage_id', (int)$activity_group_stage_id)
            ->field($field)
            ->page($page, $size)
            ->order('create_at desc')
            ->select();
        // 将时间戳转换成时间格式
        foreach ($result as $value) {
            $value['set_out_time'] = date('Y-m-d H:i', $value['set_out_time']);
        }
        $total = $result->count();
        return Functions::result(
            STATUS_CODE::SUCCESS,
            [
                'total' => $total,
                'list'  => $result,
            ]
        );
    }




    /**
     * @purpose 通过上车集合点ID修改上车集合点
     * @param array $data
     * @return array
     */
    public static function collectionPointEdit(array $data): array
    {
        if (!isset($data['id']) || empty($data['id'])) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '缺少上车集合点ID!'
            );
        }
        // 获取修改信息
        $collection_point_data = [
            'address'       => $data['address'],
            'lng'           => $data['lng'],
            'lat'           => $data['lat'],
            'title'         => $data['title'],
            'set_out_time'  => strtotime($data['set_out_time']),
            'sign_up_limit' => $data['sign_up_limit'],
        ];
        $model                 = new \app\model\ActivityCollectionPoint();
        $result                = $model
            ->isUpdate(true)
            ->save($collection_point_data, ['id' => $data['id']]);
        if ($result) {
            return Functions::result(
                STATUS_CODE::SUCCESS,
                '上车集合点修改成功!'
            );
        }
        return Functions::result(
            STATUS_CODE::FAIL,
            '上车集合点团期修改失败!'
        );
    }

    /**
     * @purpose 删除上车集合点
     * @param $id
     * @return array
     * @throws \Exception
     */
    public static function activityDelete(int $id): array
    {
        $model  = new \app\model\Activity();
        $result = $model
            ->destroy($id);
        if ($result) {
            return Functions::result(
                STATUS_CODE::SUCCESS,
                '活动删除成功'
            );
        }
        return Functions::result(
            STATUS_CODE::FAIL,
            '活动删除失败'
        );
    }

    /**
     * @purpose 通过团期ID删除数据(软删除)
     * @param int $id
     * @return array
     */
    public static function groupStageDelete(int $id): array
    {
        $model  = new \app\model\ActivityGroupStage();
        $result = $model
            ->destroy($id);
        if ($result) {
            return Functions::result(
                STATUS_CODE::SUCCESS,
                '团期删除成功'
            );
        }
        return Functions::result(
            STATUS_CODE::FAIL,
            '团期删除失败'
        );
    }

    /**
     * @purpose 删除上车集合点(软删除)
     * @param int $id
     * @return array
     */
    public static function collectionPointDelete(int $id): array
    {
        $model  = new \app\model\ActivityCollectionPoint();
        $result = $model
            ->destroy($id);
        if ($result) {
            return Functions::result(
                STATUS_CODE::SUCCESS,
                '上车集合点删除成功'
            );
        }
        return Functions::result(
            STATUS_CODE::FAIL,
            '上车集合点删除失败'
        );
    }
}